const config = {
    serverHost: 'http://localhost:8089/'
}

var _util = {

    request: function (param) {
        var _that = this
        $.ajax({
            type: param.method || 'GET',
            url: param.url || '',
            dataType: param.type || 'json',
            data: param.data || '',
            success: function(res) {
                if(0 === res.status) {
                    typeof param.success === 'function' && param.success(res.data, res.msg);
                }
                else if(10 === res.status) {
                    _that.doLogin();
                }
                else if(1 === res.status) {
                    typeof param.error === 'function' && param.error(res.msg);
                }
            },
            error: function(errMsg) {
                //todo: 请求错误的提示
            }
        });
    },
    doLogin: function() {
        window.location.href = './account-login.html?redirect='+encodeURIComponent(window.location.href)
    },
    getServerURL: function(path) {
        return config.serverHost + path;
    }
};

module.exports = _util